package com.handpay.zztong.hp.accountcenter.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.handpay.framework.ClientEngine;
import com.handpay.framework.HttpEngine;
import com.handpay.framework.LuaTableUtil;
import com.handpay.framework.SecureManager;
import com.handpay.framework.utils.CommonUtils;
import com.handpay.zztong.hp.R;
import com.handpay.zztong.hp.base.activity.ZZTongActivity;
import com.handpay.zztong.hp.bean.ActionBar;
import com.handpay.zztong.hp.bean.ResponseBean;
import com.handpay.zztong.hp.log.HPLog;
import com.handpay.zztong.hp.notification.model.MerchantInfo;
import com.handpay.zztong.hp.protocol.BaseProtocolKey;
import com.handpay.zztong.hp.protocol.UCProtocolKey;
import com.handpay.zztong.hp.utils.AccountUtils;
import com.handpay.zztong.hp.utils.BankUtils;

import java.util.Hashtable;

import se.krka.kahlua.vm.LuaTable;

/**
 * 收款卡管理
 *
 * @author jjyang
 */
public class BankCardManagerActivity extends ZZTongActivity {

    private ImageView bank_type_logo;
    private Button modify_card;
    private TextView bank_name, cardholder_name, cardNotv, bank_address;
    private String mBankCode, mSubBankCode;
    private String mBankProvinceId, mBankAreaId;
    private String mBankProvinceName, mBankAreaName, mSubBankName;
    private String cardNum;

    protected boolean isShownTabs() {
        return false;
    }

    @Override
    public ActionBar getActivityActionBar() {
        return new ActionBar(this.getString(R.string.Receivables_management), true);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_bank_card_manager);
        super.onCreate(savedInstanceState);

        bank_name = (TextView) findViewById(R.id.bank_name);
        cardholder_name = (TextView) findViewById(R.id.cardholder_name);
        cardNotv = (TextView) findViewById(R.id.cardNotv);
        bank_address = (TextView) findViewById(R.id.bank_address);
        bank_type_logo = (ImageView) findViewById(R.id.bank_type_logo);
        modify_card = (Button) findViewById(R.id.modify_cardbt);
        modify_card.setOnClickListener(this);
        if (AccountUtils.getAccountStatus(this) != AccountUtils.AccountStatus.SUCCESS) {
            modify_card.setVisibility(View.GONE);
        }
    }

    protected void onResume() {
        super.onResume();
        getMerchantInfo();
    }

    private void getMerchantInfo() {
        Hashtable<String, String> params = new Hashtable<String, String>();
        params.put(BaseProtocolKey.ACCOUNT, SecureManager.getInstance().des(AccountUtils.getAccountName(this), 1, null));
        params.put(BaseProtocolKey.CHANNEL, ClientEngine.CHANNEL);
        params.put(BaseProtocolKey.NEWBANKFLAG, newBankFlag);
        this.doJsonPost(this, DO_zztGetMerchantInfo, MerchantInfo.class, params);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        int i = v.getId();
        if (i == R.id.modify_cardbt) {
            if (mBankCode == null || mBankCode.trim().length() == 0) {
                showAlertDialog(this, getString(R.string.tip), getString(R.string.update_Bank_message), true, null);
                return;
            }
            Intent intent = new Intent(this, BankCardModifyActivity.class);
            intent.putExtra(UCProtocolKey.KEY_NAME, cardholder_name.getText());
            intent.putExtra(UCProtocolKey.KEY_BANKCODE, mBankCode);
            intent.putExtra(UCProtocolKey.KEY_BANKNAME, bank_name.getText());
            intent.putExtra(UCProtocolKey.KEY_CARDNUM, cardNotv.getText());
            if (!TextUtils.isEmpty(mBankProvinceId) && !TextUtils.isEmpty(mBankProvinceName)) {
                intent.putExtra(UCProtocolKey.KEY_PROVIVCEID, mBankProvinceId);
                intent.putExtra(UCProtocolKey.KEY_PROVINCENAME, mBankProvinceName);
            }
            if (!TextUtils.isEmpty(mBankAreaId) && !TextUtils.isEmpty(mBankAreaName)) {
                intent.putExtra(UCProtocolKey.KEY_AREAID, mBankAreaId);
                intent.putExtra(UCProtocolKey.KEY_AREANAME, mBankAreaName);
            }
            if (!TextUtils.isEmpty(mSubBankCode) && !TextUtils.isEmpty(mSubBankName)) {
                intent.putExtra(UCProtocolKey.KEY_SUBBANKNAME, mSubBankName);
                intent.putExtra(UCProtocolKey.KEY_SUBBANKCODE, mSubBankCode);
            }
            this.startActivity(intent);
        }
    }

    @Override
    public boolean netJsonResponse(String action, ResponseBean resp, boolean showAlert) {
        boolean result = super.netJsonResponse(action, resp, showAlert);
        if (result == true) {
            this.setResult(Activity.RESULT_CANCELED);
            this.closeProgressDialog();
            return true;
        }
        if (action.equals(DO_zztGetMerchantInfo)) {
            MerchantInfo bean = (MerchantInfo)resp;
//            byte data[] = (byte[]) resp.get(HttpEngine.Key_RespData);
//            LuaTable luaTable = LuaTableUtil.stringToLuaTable(data, isDataCompile);
            String bankName =  bean.getBankName();
            mSubBankName =  bean.getSubBranchName();
            cardNum =  bean.getCardNum();
            String name =  bean.getName();
            mBankProvinceId =  bean.getProvinceId();
            mBankProvinceName = bean.getProvinceName();
            if (!TextUtils.isEmpty(mBankProvinceId)) {
                mBankProvinceName = SecureManager.getInstance().des(mBankProvinceName, 0, null);
            }
            mBankAreaId =  bean.getAreaId();
            mBankAreaName = bean.getAreaName();
            if (!TextUtils.isEmpty(mBankAreaId)) {
                mBankAreaName = SecureManager.getInstance().des(mBankAreaName, 0, null);
            }

            mBankCode = bean.getBankCode();
            mSubBankCode =bean.getSubBankCode();
            name = SecureManager.getInstance().des(name, 0, null);
            bankName = SecureManager.getInstance().des(bankName, 0, null);
            HPLog.e("BankCardManagerActivity", "bankName" + bankName);
            cardNum = SecureManager.getInstance().des(cardNum, 0, null);
            if (!TextUtils.isEmpty(mSubBankCode)) {
                mSubBankName = SecureManager.getInstance().des(mSubBankName, 0, null);
                bank_address.setText(mSubBankName);
            } else {
                bank_address.setText("");
            }
            cardholder_name.setText(name);
            bank_name.setText(bankName);
            cardNotv.setText(CommonUtils.formattingCard_4(cardNum));
            bank_type_logo.setImageResource(BankUtils.getBankIcon(bankName));
            return true;
        }

        return false;

    }

    @Override
    public boolean netResponse(String action, Hashtable<String, Object> resp,
                               boolean showAlert) {
        boolean result = super.netResponse(action, resp, showAlert);
        if (result == true) {
            this.setResult(Activity.RESULT_CANCELED);
            this.closeProgressDialog();
            return true;
        }
        if (action.equals(DO_zztGetMerchantInfo)) {
            if (!result) {
                byte data[] = (byte[]) resp.get(HttpEngine.Key_RespData);
                LuaTable luaTable = LuaTableUtil.stringToLuaTable(data, isDataCompile);
                String bankName = (String) luaTable.rawget(UCProtocolKey.KEY_BANKNAME);
                mSubBankName = (String) luaTable.rawget("subBranchName");
                cardNum = (String) luaTable.rawget(UCProtocolKey.KEY_CARDNUM);
                String name = (String) luaTable.rawget(UCProtocolKey.KEY_NAME);
                mBankProvinceId = (String) luaTable.rawget(UCProtocolKey.KEY_PROVIVCEID);
                mBankProvinceName = (String) luaTable.rawget(UCProtocolKey.KEY_PROVINCENAME);
                if (!TextUtils.isEmpty(mBankProvinceId)) {
                    mBankProvinceName = SecureManager.getInstance().des(mBankProvinceName, 0, null);
                }
                mBankAreaId = (String) luaTable.rawget(UCProtocolKey.KEY_AREAID);
                mBankAreaName = (String) luaTable.rawget(UCProtocolKey.KEY_AREANAME);
                if (!TextUtils.isEmpty(mBankAreaId)) {
                    mBankAreaName = SecureManager.getInstance().des(mBankAreaName, 0, null);
                }

                mBankCode = (String) luaTable.rawget(UCProtocolKey.KEY_BANKCODE);
                mSubBankCode = (String) luaTable.rawget(UCProtocolKey.KEY_SUBBANKCODE);
                name = SecureManager.getInstance().des(name, 0, null);
                bankName = SecureManager.getInstance().des(bankName, 0, null);
                HPLog.e("BankCardManagerActivity", "bankName" + bankName);
                cardNum = SecureManager.getInstance().des(cardNum, 0, null);
                if (!TextUtils.isEmpty(mSubBankCode)) {
                    mSubBankName = SecureManager.getInstance().des(mSubBankName, 0, null);
                    bank_address.setText(mSubBankName);
                } else {
                    bank_address.setText("");
                }
                cardholder_name.setText(name);
                bank_name.setText(bankName);
                cardNotv.setText(CommonUtils.formattingCard_4(cardNum));
                bank_type_logo.setImageResource(BankUtils.getBankIcon(bankName));
            } else {
                // 闪现1次以上"您的账号已超时"提示，最后回到"账户"管理页面，弹出最后一个"您的账号已超时"提示
                // 这种方式很挫，重构时可以考虑解决
                // cyg
                byte data[] = (byte[]) resp.get(HttpEngine.Key_RespData);
                if (null != data) {
                    LuaTable luaTable = LuaTableUtil.stringToLuaTable(data, isDataCompile);
                    if (null != luaTable) {
                        String em = (String) luaTable.rawget("errMessage");
                        if (!TextUtils.isEmpty(em)) {
                            return result;
                        }
                    }
                }
                // 没有获取到信息。回到之前的界面
                finish();
            }
            return result;
        }
        return true;
    }


}
